package com.smartwebx.ha.biz.rainwater.real.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.smartwebx.ha.biz.rainwater.real.SoilManager;
import com.smartwebx.ha.dal.assign.bean.HaArea;
import com.smartwebx.ha.dal.realtime.dao.SoilMapper;
import com.smartwebx.ha.dal.usebean.DateValue;
import com.smartwebx.ha.dal.usebean.GisWater;
import com.smartwebx.ha.dal.util.core.WaterProcessDatasAnalysis;
import com.smartwebx.ha.util.GisStationInProvOrInCity;

@Service
public class SoilManagerImpl implements SoilManager {
	@Autowired
	private SoilMapper soilDao;

	private static Map<String, String> selSlmMap = Maps.newHashMap();

	static {
		selSlmMap.put("0", "SRLSLM");
		selSlmMap.put("10", "SLM10");
		selSlmMap.put("20", "SLM20");
		selSlmMap.put("30", "SLM30");
		selSlmMap.put("40", "SLM40");
		selSlmMap.put("50", "SLM50");
		selSlmMap.put("60", "SLM60");
		selSlmMap.put("70", "SLM70");
		selSlmMap.put("80", "SLM80");
		selSlmMap.put("90", "SLM90");
		selSlmMap.put("100", "SLM100");
		selSlmMap.put("110", "SLM110");
	}

	@Override
	public List<GisWater> getAllStatMoisture(Date datePoint, String selSLMName,HaArea area) {
		String selFieldName = this.getSelSLM(selSLMName);
		List<GisWater> giswaters= soilDao.getAllStatMoistureStcdFilter(datePoint, selFieldName);
		List<GisWater> gws = new ArrayList<GisWater>();
		GisStationInProvOrInCity gisStationInProvOrInCity= new GisStationInProvOrInCity();
		for(GisWater gisWater:giswaters){
			gisWater.setInProvOrInCity(gisStationInProvOrInCity.inProvOrInCity(gisWater,area));
			if (gisWater.getVal() !=null &&gisWater.getVal() != 0d) {
				gws.add(gisWater);
			}
		}
		return giswaters;
	}

	@Override
	public List<DateValue> getSingleStatDayMProcess(String stcd,
			Date startDate, Date endDate, String selSLMName, String exKey) {
		String selFieldName = this.getSelSLM(selSLMName);
		return WaterProcessDatasAnalysis.getSingleStatDayProcess(soilDao, stcd,
				startDate, endDate, "soil-" + selFieldName + "-" + exKey);
	}

	@Override
	public List<DateValue> getPurenessSingleStatDayMProcess(String stcd,
			Date startDate, Date endDate, String selSLMName, String exKey) {
		String selFieldName = this.getSelSLM(selSLMName);

		return WaterProcessDatasAnalysis.getPurenessProcesss(soilDao, stcd,
				startDate, endDate, "soil-" + selFieldName + "-" + exKey);
	}

	@Override
	public List<DateValue> getSingleStatDayMProcess(String stcd,
			Date startDate, Date endDate, String exKey) {
		return WaterProcessDatasAnalysis.getSingleStatDayProcess(soilDao, stcd,
				startDate, endDate, "soil- -" + exKey);
	}

	/**
	 * 根据字段代号返回真正字段名
	 * 
	 * @param selSLMName
	 * @return
	 */
	private String getSelSLM(String selSLMName) {
		return selSLMName == null ? "SLM20" : selSlmMap.get(selSLMName);
	}

}
